package chatGUI;
	import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import chatControler.ChatControler;
import chatModel.UtilisateurDistant;

	
	public class EcranConnexion extends JFrame implements ActionListener, WindowListener {
		
		protected JLabel lBienvenu ;
		
		//sinon c'est pas beau, ca fait tout resseré, c'est bizarre ^^
		protected JLabel lRien ; 
		protected JLabel lRien1 ; 
		protected JButton bConnect ;
		protected JLabel lpseudo ;
		protected JTextField textPseudo ;
		public EcranduChat ecranChat ;
		private ChatControler chatControler;
		

		//initComponet ca initialise l'ecran
		public EcranConnexion(ChatControler chatControler){
			initComponents() ;
			addWindowListener(this);
			this.chatControler = chatControler;
		}
		private void initComponents(){
			//Titre
			this.setTitle("Fenetre de connexion au ChatSystem");

			GridLayout l = new GridLayout(5,5) ;
			this.setLayout(l) ;
			
			this.setLocation(150, 150);
			
			this.setSize(100,100);
			lBienvenu = new JLabel("Bienvenue");
			lRien = new JLabel("") ;
			lpseudo = new JLabel("Rentrez votre pseudo");	
			textPseudo = new JTextField("");
			lRien1 = new JLabel("");
			bConnect = new JButton("Se connecter") ;	
			
			//Et les ajoute
			this.add(lBienvenu);
			this.add(lRien);
			this.add(lpseudo);
			this.add(textPseudo);
			this.add(lRien1); 
			this.add(bConnect);
			
			//quand on appuie sur entrée ça selecitonner le bouton de connexion
			this.getRootPane().setDefaultButton(bConnect) ;
			this.pack() ;
			this.setVisible(true);
			bConnect.addActionListener(this);
	}
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == bConnect) 
		      {
				chatControler.getLocalUser().setUserName(textPseudo.getText());
				try {
					chatControler.getLocalUser().setMyIp(InetAddress.getLocalHost());
				} catch (UnknownHostException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				UtilisateurDistant allUsers=null;
				try {
					allUsers = new UtilisateurDistant("All Users", InetAddress.getByName("255.255.255.255"));
				} catch (UnknownHostException e1) {
					e1.printStackTrace();
				}
				chatControler.getOtherUserList().add(allUsers);
				chatControler.setConnected(true);
		      }
			
			
		}
		@Override
		public void windowActivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void windowClosed(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
		@Override
		public void windowDeactivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void windowDeiconified(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void windowIconified(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void windowOpened(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}

}
